from py2neo import Node, Relationship

from src.database.connection import Neo4jInstance
from src.ifc.rule.task_type import *


# 梁任务
def beam_task(storeys):
    for index, storey in enumerate(storeys):
        tx = Neo4jInstance.get_connect().begin()
        storey_id = storey["GlobalId"]
        components = tx.run(
            """
            MATCH (c:IfcBeam)-[:PLACED_IN]->(s:IfcBuildingStorey {GlobalId: $storey_id}) 
            RETURN c
        """,
            storey_id=storey_id,
        ).data()
        components = [component["c"] for component in components]
        for component in components:
            task_steel_node = Node(
                TaskBeamSteel,
                Name="梁钢筋绑扎",
                Layer=storey["Name"],
                Workload=2,
            )
            task_board_node = Node(
                TaskBeamBoard,
                Name="梁模板",
                Layer=storey["Name"],
                Workload=2,
            )
            task_concrete_node = Node(
                TaskBeamConcrete,
                Name="梁混凝土",
                Layer=storey["Name"],
                Workload=component["volume"] * 0.164,
            )
            task_maintain_node = Node(
                TaskBeamMaintain,
                Name="梁养护",
                Layer=storey["Name"],
                Workload=10 if component["span"] < 8000 else 14,
            )
            rel_steel = Relationship(task_steel_node, "FOR", component)
            rel_board = Relationship(task_board_node, "FOR", component)
            rel_concrete = Relationship(task_concrete_node, "FOR", component)
            rel_maintain = Relationship(task_maintain_node, "FOR", component)
            tx.create(rel_steel)
            tx.create(rel_board)
            tx.create(rel_concrete)
            tx.create(rel_maintain)

            # 梁和楼板，是先做模板，再钢筋绑扎
            rel_steel_follow_board = Relationship(
                task_steel_node, "FOLLOW", task_board_node
            )
            rel_concrete_follow_board = Relationship(
                task_concrete_node, "FOLLOW", task_steel_node
            )
            rel_maintain_follow_concrete_follow = Relationship(
                task_maintain_node, "FOLLOW", task_concrete_node
            )
            tx.create(rel_steel_follow_board)
            tx.create(rel_concrete_follow_board)
            tx.create(rel_maintain_follow_concrete_follow)
        tx.commit()
